using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CodeFactory.Library.Language;

namespace CodeFactory.UI
{
    public partial class ProgressForm : Form
    {
        public event EventHandler Runner;
        //private IWin32Window owner;

        private object data;

        public string Message
        {
            get { return this.messageLabel.Text; }
            set { this.messageLabel.Text = value; }
        }

        public string Title
        {
            get { return this.group_progess.Text; }
            set { this.group_progess.Text = value; }
        }

        public int ProgressTotal
        {
            get { return this.mainProgress.Maximum; }
            set { this.mainProgress.Maximum = value; }
        }

        public int ProgressCurrent
        {
            set { this.mainProgress.Value = value; }
            get { return this.mainProgress.Value; }
        }

        public object Data
        {
            get { return this.data; }
            set { this.data = value; }
        }

        //public IWin32Window Owner
        //{
        //    set { this.owner = value; }
        //}

        public ProgressForm()
        {
            InitializeComponent();
            this.KeyPreview = true;
            this.KeyDown += new KeyEventHandler(ProgressForm_KeyDown);
            this.FormClosing += this.Form_FormClosing;
        }

        void ProgressForm_KeyDown(object sender, KeyEventArgs e)
        {
            //throw new Exception("The method or operation is not implemented.");
            System.Threading.Thread thread=((MainForm)this.Owner).CreateThread;
            if ( thread != null)
            {
                if (thread.IsAlive)
                {
                    if (e.KeyCode == Keys.Escape)
                    {
                        this.Text = LanguageManager.StringManager["Create_Msg_Exit_Wait"];
                        thread.Abort();
                        this.Hide();
                    }
                }
            }
        }

        public void Run()
        {
            if (this.Runner != null)
                this.Runner(this, null);
            this.Hide();
        }

        public void StartRun(object sender, EventArgs e)
        {
            System.Threading.Thread runThread = new System.Threading.Thread(this.Run);
            runThread.Start();
        }

        public void UpdateCreateMessage(string table, string column, int all, int finish)
        {
            this.messageLabel.Text = string.Format(LanguageManager.StringManager["CreateMessage"], finish, all, table, column);
            this.mainProgress.Value = (int)((double)finish / (double)all * 100);
            this.mainProgress.Update();
            this.OnPaint(null);
        }

        private void Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.Threading.Thread thread = ((MainForm)this.Owner).CreateThread;
            if (thread != null)
            {
                if (thread.IsAlive)
                    e.Cancel = true;
            }
        }
    }
}